// 获取 dom 结构
const button = document.querySelector("#button");
// ajax 模块
const request = (opts) => {
    const {
        url,
        method = 'GET',
    } = opts || {};
    return new Promise((resolve, reject) => {
        try {
            const xhr = new XMLHttpRequest();
            xhr.open(method, url, true);
            xhr.addEventListener('readystatechange', function () {
                if(xhr.readyState === 4) {
                    if((xhr.status >= 200 && xhr.status < 300) || xhr.status === 302) {
                        resolve(xhr.response);
                    }
                }
            }, false);
            xhr.send();
        } catch (error) {
            console.log(error);
            reject(error);
        }
    });
};
// 点击事件回调
const onClickGetData = async(e) => {
    try {
        const result = await request({
            url: 'http://localhost:3001/getData',
        });
        console.log("result::", result);
    } catch (error) {   
        console.log(error);
    }
};
// 点击点击事件监听
button.addEventListener('click', onClickGetData, false);